package cn.hls.web.controller.user;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hls.entity.Role;
import cn.hls.entity.User;
import cn.hls.entity.UserRole;
import cn.hls.entity.vo.Msg;
import cn.hls.entity.vo.PageInfo;
import cn.hls.service.RoleService;
import cn.hls.service.UserRoleService;
import cn.hls.service.UserService;
import cn.hls.until.GenericsUtils;
import cn.hls.web.controller.base.BaseController;

 

@Controller
public class UserController extends BaseController<User> {
	@Resource(name = "userService")
	private UserService userService;
	@Resource(name = "userRoleService")
	private UserRoleService userRoleService;
	@Resource(name = "roleService")
	private RoleService roleService;

	@RequestMapping("/login")
	public String login(HttpRequest request) {
		return "/user/login";
	}

	@RequestMapping("user/logining")
	@ResponseBody
	public Msg logining(User entity, String chkcode, HttpServletRequest request)
			throws Exception {
		/** 验证码校验 **/
		if (chkcode == null) {
			msg.setInfo("验证码不能为空!");
			msg.setStatus(401);
		}
		if (request.getSession().getAttribute("randomCode")==null||
				!chkcode.equalsIgnoreCase(request.getSession()
				.getAttribute("randomCode").toString())) {
			msg.setInfo("验证码不正确!");
			msg.setStatus(401);
		} else {
			/**
			 * 判断用户信息
			 */
			User user = userService.login(entity.getLoginName());
			if (user != null) {
				//比较密码
				if (GenericsUtils.encrypt(GenericsUtils.encrypt(entity.getPassword(),user.getSalt()),null)
						.equals(user.getPassword())) {
					// 设置user session
					msg.setStatus(200);
					request.getSession().setAttribute("currentUser", user);
				} else {
					msg.setStatus(402);
					msg.setInfo("密码不正确!");
				}
			} else {
				msg.setStatus(402);
				msg.setInfo("用户名不存在");
			}
		}
		return msg;
	}

	@RequestMapping("/user/logOut")
	public String logOut(HttpServletRequest request) throws Exception {
		request.getSession().invalidate();
		// 重定向登录页面
		return "redirect:/login.jsp";
	}

	@RequestMapping("/user/updatePwd")
	@ResponseBody
	public Msg updatePwd(User entity) throws Exception {
		// 密码加密处理
		String salt=GenericsUtils.getSalt();
		entity.setSalt(salt);
		entity.setPassword(GenericsUtils.encrypt(GenericsUtils.encrypt(entity.getPassword(),salt),null));
		boolean flag = userService.updatePwd(entity);
		if (flag) {
			msg.setStatus(200);
			msg.setInfo("操作成功");
		} else {
			msg.setStatus(201);
			msg.setInfo("操作失败");
		}
		return msg;
	}

	/**
	 * 加载用户所拥有的角色信息
	 * 
	 * @param page
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/user/list")
	@ResponseBody
	public PageInfo<Role> list(PageInfo<UserRole> page, Long roleId,
			Long userId) throws Exception {
		PageInfo<UserRole> pageInfo = userRoleService.loadUserRoleInfo(page,
				roleId, userId);
		// 定义返回的结果集
		PageInfo<Role> result = new PageInfo<Role>();
		// 定义Iterator变量
		Iterator<UserRole> iterator = pageInfo.getList().iterator();
		List<Role> list = new ArrayList<Role>();
		while (iterator.hasNext()) {
			UserRole tempEntity = (UserRole) iterator.next();
			list.add(tempEntity.getRole());
		}
		/**
		 * 设置result分页信息
		 */
		result.setResults(pageInfo.getResults());
		result.setSuccess(pageInfo.isSuccess());
		result.setStart(pageInfo.getStart());
		result.setLimit(pageInfo.getLimit());
		result.setList(list);
		return result;
	}

	/**
	 * 更新用户角色信息
	 * 
	 * @param userId
	 *            用户编号
	 * @param roleId
	 *            角色编号
	 * @return Msg
	 * @throws Exception
	 */
	@RequestMapping("/user/updateUserRole")
	@ResponseBody
	public Msg updateUserRole(Long userId, Long roleId) throws Exception {
		UserRole entity = new UserRole();
		User user = userService.find(userId);
		Role role = roleService.find(roleId);
		entity.setUser(user);
		entity.setRole(role);
		boolean flag = false;
		flag = userRoleService.add(entity);
		if (flag) {
			msg.setInfo("角色分配成功");
			msg.setStatus(200);
		} else {
			msg.setInfo("角色分配失败");
			msg.setStatus(201);
		}
		return msg;
	}

	/**
	 * 撤消用户的角色信息
	 * 
	 * @param userId
	 *            用户编号
	 * @param roleId
	 *            角色编号
	 */
	@RequestMapping("/user/deleteUserRole")
	@ResponseBody
	public Msg deleteUserRole(Long roleId, Long userId) throws Exception {
		userRoleService.deleteUserRole(roleId, userId);
		msg.setInfo("角色撤消成功");
		msg.setStatus(200);
		return msg;
	}
}
